/**
 * 
 */
package captor.windowsystem.project.newproject.cards;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import captor.crosscuttingdomainsystem.CrosscuttingDomain;
import captor.domainsystem.BaseDomain;
import captor.domainsystem.DomainException;
import captor.domainsystem.BaseDomain.BaseDomainException;

import captor.lib.intl.MyIntl;
import captor.modelsystem.Model;
import captor.modelsystem.Project;
import captor.windowsystem.util.DomainCheckBox;

/**
 * @author Carlos
 *
 */

public class SelectCrosscuttingDomains extends FITCard {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = -9035848541698385885L;
	private JPanel cardHeader;
	private JPanel cardBody;
	private Vector<DomainCheckBox> vectorDomainCheckBox;	

	public SelectCrosscuttingDomains(Model model, Project project){
		super(model,project);
	}
	
	/* (non-Javadoc)
	 * @see captor.windowsystem.project.newproject.cards.FITCard#create()
	 */
	
	@Override
	public void create() {
		// TODO Auto-generated method stub
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createRigidArea(new Dimension(5,20)));
		
		cardHeader = createCardHeader();
        add(cardHeader);
        
        add(Box.createRigidArea(new Dimension(5,20)));
        
        vectorDomainCheckBox = new Vector<DomainCheckBox>();
        cardBody = createCardBody();
        add(cardBody);
                        
	}

	/* (non-Javadoc)
	 * @see captor.windowsystem.project.newproject.cards.FITCard#validateFields()
	 */
	@Override
	public boolean validateFields() {
		// TODO Auto-generated method stub
		return true;
	}

	private JPanel createCardHeader(){
		
		JLabel lblDomainBase, lblDomainBaseName;
		Color c = Color.BLUE;
        c = c.darker();				       
        
        lblDomainBase = new JLabel(MyIntl.MSG116);
        lblDomainBase.setFont(new Font("Default", Font.BOLD, 12));
        lblDomainBase.setForeground(c);
        
        lblDomainBaseName = new JLabel(project.getDomain());
        
        JPanel header = new JPanel();                   
        header.setLayout(new BoxLayout(header, BoxLayout.X_AXIS));
        header.add(lblDomainBase);
        header.add(lblDomainBaseName);
        header.add(Box.createHorizontalGlue());
        
        return header;
	}
	
	private JPanel createCardBody(){
		
		JPanel body = new JPanel();		
		body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
					
		BaseDomain baseDomain = new BaseDomain(model,project.getDomain());
		
		try {
			baseDomain.validateDomain();
		}
		catch (BaseDomainException e1) {
			body.add(new JLabel(MyIntl.MSG117));
			return body;
		}
		catch (DomainException e2) {
			body.add(new JLabel(e2.getMessage()));
			return body;
		}
		
		boolean flagXmlInfoFile = true;
		if (!baseDomain.loadCompatibleCrosscutingDomains(flagXmlInfoFile)){
			body.add(new JLabel(MyIntl.MSG118));
			return body;
		}
		
		List<CrosscuttingDomain> compatibleDomainsList= baseDomain.getCompatibleDomains();
		
		vectorDomainCheckBox = new Vector<DomainCheckBox>();
		DomainCheckBox item;
		
		for(CrosscuttingDomain crosscuttingDomain : compatibleDomainsList) {
		    try{
		    	crosscuttingDomain.validateDomain();
		    	item = new DomainCheckBox(crosscuttingDomain.getNameDomain(), crosscuttingDomain );
			    vectorDomainCheckBox.add(item);
		    }
		    catch (DomainException e1){
		    	String msg = MyIntl.MSG119;	
		    	msg = msg.concat(e1.getMessage());
		    	item = new DomainCheckBox(crosscuttingDomain.getNameDomain()+msg);
		    	item.setEnabled(false);
		    }		    		   
		    body.add(item);
		}		
		return body;
	}
	
	public List<CrosscuttingDomain> getSelectedCrosscuttingDomains(){
		List<CrosscuttingDomain> selectedCrosscuttingDomains = new LinkedList<CrosscuttingDomain>();
		
		if (!vectorDomainCheckBox.isEmpty()){			
			for(DomainCheckBox domainCheckBox:vectorDomainCheckBox){
				if (domainCheckBox.isSelected()){
					selectedCrosscuttingDomains.add(domainCheckBox.getCrosscuttingDomain());
				}					
			}
		}
		return selectedCrosscuttingDomains;		
	}
}